package com.vf.admin.miaodong.controller;

import com.vf.admin.client.service.ExpressionsCommonService;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.ExpressionsCommon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/after/expressionsCommon")
public class AfterExpressionsController extends BaseController {

    @Autowired
    ExpressionsCommonService expressionsCommonService;

    @RequestMapping("/list")
    public String list(SplitPageBean page, Integer sex) {
        try {
            Map<String, Object> params = new HashMap<>();
            if(sex != null) {
                params.put("sex", sex);
                request.setAttribute("sex", sex);
            }
            List<ExpressionsCommon> list = expressionsCommonService.page(page, params);
            request.setAttribute("list", list);
            request.setAttribute("splitPage", page);
            return this.sucess("/web/after/expressionsCommon/list");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Result saveOrUpdate(ExpressionsCommon params) {
        try {
            if (StringUtils.isEmpty(params.getContent())) {
                return errorJSON("内容不能为空");
            }
            if (params.getSex() == null) {
                return errorJSON("性别不能为空");
            }
            expressionsCommonService.saveOrUpdate(params);
            return sucessJSON("成功");
        } catch (Exception e) {
            return errorJSON("系统繁忙");
        }
    }

    @RequestMapping("/info")
    public String info(Integer id) {
        try {
            ExpressionsCommon info = expressionsCommonService.info(id);
            request.setAttribute("obj", info);
            return this.sucess("/web/after/expressionsCommon/edit");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }

    @ResponseBody
    @RequestMapping("/del")
    public Result del(Integer id) {
        expressionsCommonService.del(id);
        return sucessJSON("成功");
    }

    @RequestMapping("/gotoAdd")
    public String gotoAdd() {
        return this.sucess("/web/after/expressionsCommon/add");
    }
}
